package cn.legend.travel.admin.authorize.dao.persist.repository.impl;

import cn.legend.travel.admin.authorize.dao.persist.mapper.RoleMapper;
import cn.legend.travel.admin.authorize.dao.persist.repository.IRoleRepository;
import cn.legend.travel.admin.authorize.pojo.entity.Role;
import cn.legend.travel.admin.authorize.pojo.param.RoleAddNewParam;
import cn.legend.travel.admin.authorize.pojo.param.RoleUpdateParam;
import cn.legend.travel.admin.authorize.pojo.vo.RoleAndPermissionVO;
import cn.legend.travel.admin.authorize.pojo.vo.RoleStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class RoleRepositoryImpl implements IRoleRepository {
    private final RoleMapper roleMapper;

    @Override
    public RoleStandardVO getRoleById(Long roleId) {
        return roleMapper.getRoleById(roleId);
    }

    @Override
    public List<RoleAndPermissionVO> getAll() {
        return roleMapper.getAll();
    }

    @Override
    public int addNew(RoleAddNewParam param) {
        Role role = new Role();
        BeanUtils.copyProperties(param, role);
        return roleMapper.insert(role);
    }

    @Override
    public int update(RoleUpdateParam param) {
        Role role = new Role();
        BeanUtils.copyProperties(param, role);
        return roleMapper.updateById(role);
    }

    @Override
    @Transactional
    public int delete(Long roleId) {
        return roleMapper.deleteById(roleId);
    }

    @Override
    public Role getRoleByName(String name) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("name", name);
        return roleMapper.selectOne(wrapper);
    }
}
